package com.rt.tmpt.controller.springmvc.admin;


import com.googlecode.genericdao.search.Search;
import com.rt.tmpt.utils.JsonPaginationUtils;
import com.rt.tmpt.dto.A1OperationlogDto;
import com.rt.tmpt.dto.PageDto;
import com.rt.tmpt.dto.PaginationDto;
import com.rt.tmpt.service.IOperationlogService;
import com.rt.tmpt.utils.DateTool;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@ApiIgnore
@Controller
@RequestMapping("/admin123/operationlog")
public class OperationlogAjaxAction{

    @Resource
    IOperationlogService operationlogService;

	/**
	 * 分页查询操作日志
	 * @param loginName
	 * @param logTypeId
	 * @param orderTypeId
	 * @param orderNum
	 * @param recordDateStart
	 * @param recordDateEnd
	 * @param pageDto
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/operationlogAjax_queryOperationlogBySearch")
	@ResponseBody
	public String queryOperationlogBySearch(String loginName, int logTypeId, int orderTypeId,
                                            String orderNum, String recordDateStart, String recordDateEnd,
                                            PageDto pageDto,
                                            HttpServletRequest request, HttpServletResponse response){
		String r;

		Search search = new Search();
		search.setPage(pageDto.getPageCurrent()-1);
		search.setMaxResults(pageDto.getPageSize());

		String sortOrder = pageDto.getSortOrder();
		String sortColumn = pageDto.getSortColumn();
		if("desc".equals(sortOrder) && sortColumn!=null){
			search.addSort(sortColumn, true);
		}else if("asc".equals(sortOrder) && sortColumn!=null){
			search.addSort(sortColumn, false);
		}

		if(loginName!= null && loginName.trim() != ""){
			search.addFilterEqual("loginName", loginName);
		}
		if(logTypeId!=0){
			search.addFilterEqual("logType.id",logTypeId);
		}
		if(orderTypeId !=0){
			search.addFilterEqual("orderType", orderTypeId);
		}
		if(orderNum != null && orderNum.trim() != ""){
			search.addFilterEqual("orderNum", orderNum);
		}
		search.addFilterGreaterOrEqual("createDate", DateTool.string2Date(recordDateStart, "yyyy-MM-dd"));
		search.addFilterLessOrEqual("createDate", DateTool.string2DateEnd(recordDateEnd, "yyyy-MM-dd"));

		if(JsonPaginationUtils.checkDateResponseWriter(recordDateStart, recordDateEnd, response)){
			return "jsonResult";
		}
		
		HttpSession session = request.getSession();
		session.removeAttribute("logListSearch");
		session.setAttribute("logListSearch", search.copy());

		PaginationDto<A1OperationlogDto> pagination = operationlogService.paginationBySearch(search);

		r = JsonPaginationUtils.paginationToJson(pagination);

		JsonPaginationUtils.responseWriter(response, r);
		
		return null;
	}

	
}
